package com.sc.syllabus.decoration;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sc.syllabus.R;
import com.sc.syllabus.decoration.bean.HouseDecorationBean;
import com.sc.syllabus.model.TypeId;

import java.util.List;

/**
 * Created by liushuanglong on 2017/9/20.
 */

public class DecorationListAdapter extends BaseAdapter {

    private Context context;
    private List<HouseDecorationBean> data;
    private LayoutInflater mInflater;

    public DecorationListAdapter(Context context, List<HouseDecorationBean> data) {
        this.context = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (null != data) {
            return data.size();
        }
        return 0;
    }

    @Override
    public HouseDecorationBean getItem(int position) {
        if (null != data) {
            return data.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_decoration, null);
            holder.textView = (TextView) convertView.findViewById(R.id.text);
            holder.textAmount = (TextView) convertView.findViewById(R.id.text1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (null != data && !data.isEmpty()) {
            HouseDecorationBean bean = data.get(position);
            String title = bean.getTitle() + "\n";
            String ePrice = "预计价格:" + bean.getExpectPrice();
            String eNum = "\n预计数量:" + bean.getExpectNum();
            String aPrice = "\n实际价格:" + bean.getActualPrice();
            String aNum = "\n实际数量:" + bean.getActualNum();
            String type = "\n类    型:" + TypeId.typeTitles[bean.getType()];
            holder.textView.setText(title + ePrice + eNum + aPrice + aNum + type);
            holder.textAmount.setText("计划总额:" + bean.getExpectPrice() * bean.getExpectNum() + "\n" +
                    "实际总额:" + bean.getActualPrice() * bean.getActualNum());
        }
        return convertView;
    }

    private class ViewHolder {
        TextView textView;
        TextView textAmount;
    }

    public void setData(List<HouseDecorationBean> data) {
        if (null != data && !data.isEmpty()) {
            this.data.clear();
        }
        this.data = data;
        this.notifyDataSetInvalidated();
    }

    public void remove(int position) {
        this.data.remove(position);
        this.notifyDataSetInvalidated();
    }
}
